/*
 * NetworkLinkJPanel.java
 *
 * Created on Utorok, 2007, november 6, 14:01
 */

package gui;

import logic.topology.NetworkLink;
import logic.topology.NetworkNode;

/**
 *
 * @author  handsome
 */
public class NetworkLinkJPanel extends ParametersPanel {
    
    NetworkLink NetworkL = null;
   
            
    /** Creates new form NetworkLinkJPanel */
    public NetworkLinkJPanel() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        NameJTxtField = new javax.swing.JTextField();
        NameJLbl = new javax.swing.JLabel();
        BoudRateJLbl = new javax.swing.JLabel();
        MTUJLbl = new javax.swing.JLabel();
        PropDelayJLbl = new javax.swing.JLabel();
        BoudRateJTxtField = new javax.swing.JTextField();
        MTUJTxtField = new javax.swing.JTextField();
        PropDelayJTxtField = new javax.swing.JTextField();

        NameJTxtField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NameJTxtFieldActionPerformed(evt);
            }
        });
        NameJTxtField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                NameJTxtFieldFocusLost(evt);
            }
        });

        NameJLbl.setText("Name :");

        BoudRateJLbl.setText("Baud rate :");

        MTUJLbl.setText("MTU :");

        PropDelayJLbl.setText("Propagation delay :");

        BoudRateJTxtField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BoudRateJTxtFieldActionPerformed(evt);
            }
        });
        BoudRateJTxtField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                BoudRateJTxtFieldFocusLost(evt);
            }
        });

        MTUJTxtField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MTUJTxtFieldActionPerformed(evt);
            }
        });
        MTUJTxtField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                MTUJTxtFieldFocusLost(evt);
            }
        });

        PropDelayJTxtField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PropDelayJTxtFieldActionPerformed(evt);
            }
        });
        PropDelayJTxtField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                PropDelayJTxtFieldFocusLost(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(BoudRateJLbl)
                    .addComponent(NameJLbl)
                    .addComponent(MTUJLbl)
                    .addComponent(PropDelayJLbl))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(NameJTxtField, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(PropDelayJTxtField, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(MTUJTxtField, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(BoudRateJTxtField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 48, Short.MAX_VALUE)))
                .addContainerGap(63, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(NameJTxtField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(NameJLbl))
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(BoudRateJTxtField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(MTUJLbl)
                            .addComponent(MTUJTxtField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(22, 22, 22)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(PropDelayJLbl)
                            .addComponent(PropDelayJTxtField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(BoudRateJLbl))
                .addContainerGap(235, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void PropDelayJTxtFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_PropDelayJTxtFieldFocusLost
        propDelaySet();
    }//GEN-LAST:event_PropDelayJTxtFieldFocusLost

    private void MTUJTxtFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_MTUJTxtFieldFocusLost
        mtuSet();
    }//GEN-LAST:event_MTUJTxtFieldFocusLost

    private void BoudRateJTxtFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_BoudRateJTxtFieldFocusLost
        boudRateSet();
    }//GEN-LAST:event_BoudRateJTxtFieldFocusLost

    private void NameJTxtFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_NameJTxtFieldFocusLost
        nameSet();
    }//GEN-LAST:event_NameJTxtFieldFocusLost

    /**
     * Nastavuje pre Network Link parameter Propagation delay (oneskorenie prechodu)
     */
    private void propDelaySet(){
        Double PropDelay;         
        try {
            PropDelay = Double.valueOf(PropDelayJTxtField.getText());
            if (PropDelay >= 0) {
                NetworkL.setPropagationDelay(PropDelay);                
            }else GangstersGrid.showErrorMessage("Value of 'Propagation delay' must be zero or greater");            
        }catch (Exception NumberFormatException) {
                PropDelayJTxtField.setText(Double.valueOf(NetworkL.getPropagationDelay()).toString());
                GangstersGrid.showErrorMessage("'Propagation delay' must be a number");
            }        
    }
    
    private void PropDelayJTxtFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PropDelayJTxtFieldActionPerformed
        propDelaySet();
    }//GEN-LAST:event_PropDelayJTxtFieldActionPerformed

    /**
     * Nastavuje pre Network Link paramter MTU - Maximum Transimission Unit  (Maximalna naraz velkost prenasnej jednotky)
     */
    private void mtuSet(){
        Integer mtu;         
        try {
            mtu = Integer.valueOf(MTUJTxtField.getText());
            if (mtu >= 0) {
                NetworkL.setMtu(mtu);                
            }else GangstersGrid.showErrorMessage("Value of 'MTU' must be zero or greater");            
        }catch (Exception NumberFormatException) {
                MTUJTxtField.setText(Integer.valueOf(NetworkL.getMtu()).toString());
                GangstersGrid.showErrorMessage("'MTU' must be a number");
            }        
    }
        
    
    private void MTUJTxtFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MTUJTxtFieldActionPerformed
        mtuSet();
    }//GEN-LAST:event_MTUJTxtFieldActionPerformed

    /**
     * Nastavuje pre Network Link parameter Baud rate (prenosova rychlost) 
     */
    private void boudRateSet(){
        Double BoudRate;         
        try {
            BoudRate = Double.valueOf(BoudRateJTxtField.getText());
            if (BoudRate >= 0) {
                NetworkL.setBoudRate(BoudRate);                
            }else GangstersGrid.showErrorMessage("Value of 'Boud-rate' must be zero or greater");            
        }catch (Exception NumberFormatException) {
                BoudRateJTxtField.setText(Double.valueOf(NetworkL.getBoudRate()).toString());
                GangstersGrid.showErrorMessage("'Boud-rate' must be a number");
            }        
    }
    
    private void BoudRateJTxtFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BoudRateJTxtFieldActionPerformed
        boudRateSet();
    }//GEN-LAST:event_BoudRateJTxtFieldActionPerformed

    /**
     * Nastavuje pre Network Link parameter Name (Meno)
     */
    private void nameSet(){
        try {
            NetworkL.setName(NameJTxtField.getText());
        } catch (Exception ex) {
            NameJTxtField.setText(NetworkL.getName());
            GangstersGrid.showErrorMessage(ex.getMessage());
        }
    }
    
    private void NameJTxtFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NameJTxtFieldActionPerformed
        nameSet();
    }//GEN-LAST:event_NameJTxtFieldActionPerformed
    
    /**
     * Nastavuje pre Network Link vsetky potrebne paramtre
     */
    public void FillUpNetworkLDetails(){
        nameSet();
        boudRateSet();
        mtuSet();
        propDelaySet();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel BoudRateJLbl;
    private javax.swing.JTextField BoudRateJTxtField;
    private javax.swing.JLabel MTUJLbl;
    private javax.swing.JTextField MTUJTxtField;
    private javax.swing.JLabel NameJLbl;
    private javax.swing.JTextField NameJTxtField;
    private javax.swing.JLabel PropDelayJLbl;
    private javax.swing.JTextField PropDelayJTxtField;
    // End of variables declaration//GEN-END:variables

    public void setObject(Object o) {
        NetworkL = ((NetworkLink)o);
        NameJTxtField.setText(NetworkL.getName());
        BoudRateJTxtField.setText(Double.valueOf(NetworkL.getBoudRate()).toString());
        MTUJTxtField.setText(Integer.valueOf(NetworkL.getMtu()).toString());
        PropDelayJTxtField.setText(Double.valueOf(NetworkL.getPropagationDelay()).toString());        
    }

    public void saveObjectValues() {
        if (NetworkL != null){
            FillUpNetworkLDetails();
        }
    }
    
}
